/**
 * @file setting_search_switch.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SEARCH_SWTICH != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static lv_obj_t * search_switch_create(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void search_switch_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;
    lv_watch_png_cache_all_free();

    lv_obj_t * obj = search_switch_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return;
}

void search_switch_init(uint8_t search_switch)
{
    cplog_printf("%s: search_switch %d\n", __FUNCTION__, search_switch);

#if USE_LV_BLE_DEMO != 0
    if(SEARCH_SWITCH_OFF == search_switch) {
        ble_fmp_setting(false, NULL, NULL);
    } else {
        ble_fmp_setting(true, search_create_process, search_destroy_process);
    }
#endif
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void search_switch_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static void setting_set_current_search_switch(uint8_t search_switch)
{
    // nv write
    uint8_t nvdata = search_switch;
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, NV_APN_OFFSET + sizeof(nv_apn_info) * 2, 1, &nvdata);
}

static lv_searchswitch_obj_ext_t * search_switch_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_SEARCH_SWITCH);
    if(activity_obj == NULL) return NULL;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return NULL;

    if(watch_obj) {
        lv_searchswitch_obj_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

static void search_switch_off_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(event != LV_EVENT_CLICKED) return;

    lv_searchswitch_obj_ext_t * ext = search_switch_get_ext();
    LV_ASSERT_MEM(ext);

    lv_checkbox_set_checked(ext->cb_off, true);
    uint8_t search_switch;
    UI_NV_Read_Req(NV_SECTION_UI_SETTINGS, NV_APN_OFFSET + sizeof(nv_apn_info) * 2, 1, &search_switch);
    if(SEARCH_SWITCH_ON == search_switch) {
        setting_set_current_search_switch(SEARCH_SWITCH_OFF);
        lv_checkbox_set_checked(ext->cb_on, false);
#if USE_LV_BLE_DEMO != 0
        ble_fmp_setting(false, NULL, NULL);
#endif
    }
}

static void search_switch_on_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(event != LV_EVENT_CLICKED) return;

    lv_searchswitch_obj_ext_t * ext = search_switch_get_ext();
    LV_ASSERT_MEM(ext);

    lv_checkbox_set_checked(ext->cb_on, true);
    uint8_t search_switch;
    UI_NV_Read_Req(NV_SECTION_UI_SETTINGS, NV_APN_OFFSET + sizeof(nv_apn_info) * 2, 1, &search_switch);
    if(SEARCH_SWITCH_OFF == search_switch) {
        setting_set_current_search_switch(SEARCH_SWITCH_ON);
        lv_checkbox_set_checked(ext->cb_off, false);
#if USE_LV_BLE_DEMO != 0
        ble_fmp_setting(true, search_create_process, search_destroy_process);
#endif
    }
}

static lv_obj_t * search_switch_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SEARCH_SWITCH;
        activity_ext.create = search_switch_create;
        activity_ext.prepare_destory = search_switch_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_searchswitch_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_searchswitch_obj_ext_t));
    LV_ASSERT_MEM(ext);

    lv_obj_t *label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SEARCH_SWITCH);
    lv_obj_align(label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    lv_obj_t * cont = lv_cont_create(obj, NULL);
    lv_obj_add_style(cont, LV_OBJ_PART_MAIN, &lv_style_transp_fit);
    if(lv_obj_get_base_dir(obj) == LV_BIDI_DIR_RTL)
        lv_btn_set_layout(cont, LV_LAYOUT_COLUMN_RIGHT);
    else
        lv_btn_set_layout(cont, LV_LAYOUT_COLUMN_LEFT);
    lv_obj_align(cont, NULL, LV_ALIGN_CENTER, -10, -20);
    lv_cont_set_fit2(cont, LV_FIT_TIGHT, LV_FIT_TIGHT);
    lv_obj_set_click(cont, false);

    ext->cb_on = lv_checkbox_create(cont, NULL);
    lv_checkbox_set_text(ext->cb_on, lv_lang_get_text(WATCH_TEXT_ID_ON));
    lv_obj_clean_style_list(ext->cb_on, LV_CHECKBOX_PART_BULLET);
    lv_obj_add_style(ext->cb_on, LV_CHECKBOX_PART_BULLET, &lv_watch_cb_styles);
    lv_obj_set_event_cb(ext->cb_on, search_switch_on_btn_action);
    lv_watch_obj_add_element(ext->cb_on);

    ext->cb_off = lv_checkbox_create(cont, ext->cb_on);
    lv_checkbox_set_text(ext->cb_off, lv_lang_get_text(WATCH_TEXT_ID_OFF));
    lv_obj_set_event_cb(ext->cb_off, search_switch_off_btn_action);
    lv_watch_obj_add_element(ext->cb_off);
    uint8_t search_switch;
    UI_NV_Read_Req(NV_SECTION_UI_SETTINGS, NV_APN_OFFSET + sizeof(nv_apn_info) * 2, 1, &search_switch);
    if(SEARCH_SWITCH_ON == search_switch) {
        lv_checkbox_set_checked(ext->cb_on, true);
    } else {
        lv_checkbox_set_checked(ext->cb_off, true);
    }

    return obj;
}

#endif /*USE_LV_WATCH_SEARCH_SWTICH*/
